package com.ruoyi.quartz.task;

import com.ruoyi.common.utils.NoticeUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.javid.service.ToolsExpiredWarnningService;
import com.ruoyi.javid.vo.ExpiredWarnningList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 定时任务调度测试
 *
 * @author ruoyi
 */
@Component("ryTask")
public class RyTask {

    @Autowired
    ToolsExpiredWarnningService toolsExpiredWarnningService;

    public void ryMultipleParams(String s, Boolean b, Long l, Double d, Integer i) {
        System.out.println(StringUtils.format("执行多参方法： 字符串类型{}，布尔类型{}，长整型{}，浮点型{}，整形{}", s, b, l, d, i));
    }

    public void ryParams(String params) {
        System.out.println("执行有参方法：" + params);
    }

    public void ryNoParams() {
        System.out.println("执行无参方法");
    }

    public void expiredWarnningTask() {
        System.out.println("执行了钉钉提醒方法");
        // 获取数据
        List<ExpiredWarnningList> expiredWarnningLists = toolsExpiredWarnningService.expiredWarnningList();
        if (expiredWarnningLists == null || expiredWarnningLists.size() == 0) {
            System.out.println("数据为空不提醒");
            return;
        }
        // 已经过期
        List<ExpiredWarnningList> hasExpired = expiredWarnningLists.stream().filter(x -> x.getLastDays() <= 0).collect(Collectors.toList());
        // 快过期
        List<ExpiredWarnningList> soonExpired = expiredWarnningLists.stream().filter(x -> x.getLastDays() <= 7 && x.getLastDays() > 0).collect(Collectors.toList());
        if (hasExpired.size() == 0 && soonExpired.size() == 0) {
            System.out.println("数据为空不提醒");
            return;
        }
        // 组装提醒内容
        String title = "您有新的过期提醒，请查看";
        StringBuilder text = new StringBuilder();
        if (hasExpired.size() > 0) {
            text.append("### 已过期待处理：\n");
            for (ExpiredWarnningList hasEx : hasExpired) {
                text.append("- ").append(hasEx.getName()).append("\n");
            }
        }
        if (soonExpired.size() > 0) {
            text.append("### 7天内过期：\n");
            for (ExpiredWarnningList soonEx : soonExpired) {
                text.append("- ").append(soonEx.getName()).append("\n");
            }
        }
        new NoticeUtils().dingdingNotice(title, text.toString(), new ArrayList<>());
    }
}
